#include "cappmanager.h"
#include "crootwindow.h"

#include <buola/dbus/cproxy.h>

namespace buola { namespace bdesktop {

CAppManager::CAppManager(CDock *pDock)
    :   mDock(pDock)
{
    gRootWindow->sClientAdded.Connect(&CAppManager::OnClientAdded,this,_1);

    dbus::CProxy lProxy(dbus::get_session_bus(),"org.freedesktop.DBus","/org/freedesktop/DBus");
    lProxy.Connect("org.freedesktop.DBus",&CAppManager::OnDBusSignal,this,_1);
    
    os::CApplication::LoadDefaultPaths();
}

CAppManager::~CAppManager()
{
}

CApplication *CAppManager::GetApplicationForType(os::CApplication *pType)
{
    CApplication *lApp=mApplicationByType[pType];

    if(!lApp)
    {
        lApp=new CApplication(this,pType);
        
        mApplicationByType[pType]=lApp;
    }
    
    return lApp;
}

CApplication *CAppManager::GetApplicationForClass(const std::string &pClass)
{
    CApplication *lApp=mApplicationByClass[pClass];
    
    if(!lApp)
    {
        os::CApplication *lType=os::CApplication::GetByClass(pClass);
    
        if(lType)
        {
            lApp=GetApplicationForType(lType);
        }
        else
        {
            lApp=new CApplication(this,nullptr);
        }
    
        mApplicationByClass[pClass]=lApp;
    }
    
    return lApp;
}

void CAppManager::OnClientAdded(CClient *pC)
{
    CApplication *lApp=GetApplicationForClass(pC->mApplicationClass);
    lApp->OnClientAdded(pC);
}

void CAppManager::OnDBusSignal(dbus::CMessage *pMessage)
{
}

/*namespace bdesktop*/ } /*namespace buola*/ }
